package com.tanhua.server.controller;

import com.tanhua.model.domain.UserInfo;
import com.tanhua.model.enums.CommentType;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVO;
import com.tanhua.server.service.MessagesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author Xiao
 * @version 1.0
 * @description 即时消息Controller
 * @date 2021/10/30
 */
@RestController
@RequestMapping("/messages")
public class MessagesController {
    @Autowired
    private MessagesService messagesService;

    /**
     * 环信-根据环信id查询用户信息
     * @param huanxinId
     * @return
     */
    @GetMapping("/userinfo")
    public ResponseEntity userInfo(String huanxinId) {
        UserInfoVO userInfoVO = messagesService.userInfo(huanxinId);
        return ResponseEntity.ok(userInfoVO);
    }

    /**
     * 消息-添加联系人
     * @param map
     * @return
     */
    @PostMapping("/contacts")
    public ResponseEntity addContacts(@RequestBody Map map) {
        Long friendId = Long.valueOf(map.get("userId").toString());
        messagesService.addContacts(friendId);
        return ResponseEntity.ok("添加成功！");
    }

    /**
     * 消息-查询联系人列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    @GetMapping("/contacts")
    public ResponseEntity getContacts(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize,
            String keyword) {
        PageResult pageResult = messagesService.getContacts(page, pagesize, keyword);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 消息-公告列表
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/announcements")
    public ResponseEntity getAnnouncements(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        PageResult pageResult = messagesService.getAnnouncements(page, pagesize);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 消息-点赞(likes)/评论(comments)/喜欢(loves)列表
     * @param pathParam --》 likes/comments/loves
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/{pathParam}")
    public ResponseEntity getList(
            @PathVariable String pathParam,
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pagesize) {
        CommentType commentType = CommentType.LIKE;
        if ("comments".equals(pathParam)) {
            commentType = CommentType.COMMENT;
        } else if ("loves".equals(pathParam)){
            commentType = CommentType.LOVE;
        }
        PageResult pageResult = messagesService.getList(commentType, page, pagesize);
        return ResponseEntity.ok(pageResult);
    }
}
